$(() => {
    //点击详情，显示订单详情
    $("#orders table").on("click", "tbody .view.btn", function () {
        if ($(this).is(".hide-detail")) {
            $(this).removeClass("hide-detail");
            let $tr = $(this).closest("tr");
            let $detailTr = $("<tr class='order-detail'></tr>");


            let orderId = $(this).attr("id");//订单号
            let url = ctx + "/order/items";

            $.ajax({
                url,
                method: "get",
                dataType: "json",
                data: {
                    id: orderId
                },
                success(resp) {
                    if (resp.success) {
                        let items = resp.data;
                        let $detailTbl = $("<table>");//订单项详情表
                        items.forEach(t => {
                            let $tr = $("<tr>");
                            $tr.append("<td>" + t.goodName + "</td>");
                            $tr.append("<td><div style='background: url(" + (ctx + t.goodPic) + ") no-repeat center center/cover;width: 80px;height: 80px;margin:3px;'></div></td>");
                            $tr.append("<td>" + t.price + "</td>");
                            $tr.append("<td>" + t.amount + "</td>");
                            $detailTbl.append($tr);
                        });
                        $("<td colspan='6'>").append($detailTbl).appendTo($detailTr);
                    }
                }
            });

            $tr.after($detailTr);
        } else {
            console.log("b");
            $(this).addClass("hide-detail");
            $(this).closest("tr").next(".order-detail").remove();
        }

    });

    //确认收货
    $("#orders table").on("click", "tbody .confirm.btn", function () {
        let orderId = $(this).attr("id");//订单编号
        let $me = $(this);

        layui.use(function () {
            layer.confirm("是否确认对选中的订单收货?", function (idx) {
                let url = ctx + "/order/confirm";
                $.ajax({
                    url,
                    method: "patch",
                    dataType: "json",
                    data: {
                        id: orderId
                    },
                    success(resp) {
                        if (resp.success) {
                            $me.closest("tr").children().eq(3).text("已确认");
                            $me.remove();
                            layer.close(idx);
                        }
                    }
                });
            });
        });


    });

});